package vn.wallet.app.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import vn.wallet.app.dao.ICategoryDAO;
import vn.wallet.app.domain.Category;
import vn.wallet.app.domain.User;
import vn.wallet.app.helper.exceptions.TechnicalException;
import vn.wallet.app.service.ICategoryService;

@Transactional
@Service
public class CategoryService implements ICategoryService {

	@Autowired
	ICategoryDAO categoryDAO;
	private User currrenUser;

	@Override
	public Category getById(int id) throws Exception {
		throw new TechnicalException("method not implements");
	}

	@Override
	public void save(Category obj) throws Exception {
		categoryDAO.save(obj);
	}

	@Override
	public void update(Category obj) throws Exception {
		categoryDAO.update(obj);

	}

	@Override
	public void delete(Category obj) throws Exception {
		categoryDAO.delete(obj);

	}

	@Override
	public void delete(int id) throws Exception {
		throw new TechnicalException("method not implements");
	}

	@Override
	public List<Category> getAll() throws Exception {
		return categoryDAO.getAll();
	}

	@Override
	public List<Category> getAllByUserId(int userId) throws Exception {
		return categoryDAO.getAllByUserId(userId);
	}

	@Override
	public List<?> findByExample(Category obj) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(Category... t) throws Exception {
		throw new TechnicalException("Method not implemented");
	}

	@Override
	public void delete(Category... objs) throws Exception {
		throw new TechnicalException("Method not implemented");
	}

	@Override
	public void save(Category... objs) throws Exception {
		throw new TechnicalException("Method not implemented");
	}

	@Override
	public boolean isExistingCategory(Category customCategory) {
		try {
			@SuppressWarnings("rawtypes")
			List lstCategory = categoryDAO.getByName(customCategory.getName());

			if (!CollectionUtils.isEmpty(lstCategory)) {
				for (Object obj : lstCategory) {
					// compare each category
					Category cat = (Category) obj;
					if (cat.getParentId() == customCategory.getParentId()) {
						return true;
					} else {
						return false;
					}
				}
			}
		} catch (Exception ex) {
			throw new TechnicalException("Exception of isExistingCategory: " + customCategory.getName(), ex);
		}
		return false;
	}

}
